package com.htc.utils;

import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class RestClient {

	private static final int[] RequestMethod = null;
	private ArrayList<NameValuePair> headers;
	private String message;
	private ArrayList<NameValuePair> params;
	private String response;
	private int responseCode;
	private String url;

	public RestClient(String paramString) {
		this.url = paramString;
		this.params = new ArrayList<NameValuePair>();
		this.headers = new ArrayList<NameValuePair>();
	}

	private void executeRequest(HttpUriRequest paramHttpUriRequest,
			String paramString) {
		DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient();
		try {
			HttpResponse localHttpResponse = localDefaultHttpClient
					.execute(paramHttpUriRequest);
			this.responseCode = localHttpResponse.getStatusLine()
					.getStatusCode();
			this.message = localHttpResponse.getStatusLine().getReasonPhrase();
			HttpEntity localHttpEntity = localHttpResponse.getEntity();
			if (localHttpEntity != null) {
				InputStream localInputStream = localHttpEntity.getContent();
				if (this.response != null)
					localInputStream.close();
				localInputStream.close();
			}
			return;
		} catch (ClientProtocolException localClientProtocolException) {
			localDefaultHttpClient.getConnectionManager().shutdown();
			localClientProtocolException.printStackTrace();
			return;
		} catch (IOException localIOException) {
			localDefaultHttpClient.getConnectionManager().shutdown();
			localIOException.printStackTrace();
		}
	}


	public void AddHeader(String paramString1, String paramString2) {
		this.headers.add(new BasicNameValuePair(paramString1, paramString2));
	}

	public void AddParam(String paramString1, String paramString2) {
		this.params.add(new BasicNameValuePair(paramString1, paramString2));
	}

	public void Execute() throws Exception {
		String str1 = "";
		Iterator<NameValuePair> localIterator3 = null;
		HttpGet localHttpGet = null;
		Iterator<NameValuePair> localIterator2 = null;
		if (!this.params.isEmpty()) {
			str1 = str1 + "?";
			localIterator3 = this.params.iterator();
			if (localIterator3.hasNext())
				;
		} else {
			localHttpGet = new HttpGet(this.url + str1);
			localIterator2 = this.headers.iterator();
		}
		if (!localIterator2.hasNext()) {
			executeRequest(localHttpGet, this.url);
			NameValuePair localNameValuePair3 = (NameValuePair) localIterator3.next();
			String str2 = localNameValuePair3.getName()
					+ "="
					+ URLEncoder.encode(localNameValuePair3.getValue(),
							"UTF-8");
			if (str1.length() > 1) {
				str1 = str1 + "&" + str2;
			}
			str1 = str1 + str2;
		}
		NameValuePair localNameValuePair2 = (NameValuePair) localIterator2
				.next();
		localHttpGet.addHeader(localNameValuePair2.getName(),
				localNameValuePair2.getValue());

	}

	public String getErrorMessage() {
		return this.message;
	}

	public String getResponse() {
		return this.response;
	}

	public int getResponseCode() {
		return this.responseCode;
	}

}
